package control;

import lombok.Data;

import java.io.*;
import java.util.ArrayList;
@Data
public class GoodsDAO {
    String fileName = "d:/goodsInfo2.ser";
    File file;
    FileOutputStream fileOutputStream;
    ObjectOutputStream outputStream;
    ArrayList<Goods> list1 = new ArrayList<>();
    FileInputStream fileIutputStream;
    ObjectInputStream inputStream;

    public GoodsDAO() {
        try {
            file = new File(fileName);
            if (!file.exists()) {
                file.createNewFile();
                fileOutputStream = new FileOutputStream(file);
                outputStream = new ObjectOutputStream(fileOutputStream);
                Goods goods = new Goods();
                list1.add(goods);
                outputStream.writeObject(list1);
                System.out.println("文件初始化成功！");
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public void addGoods(Goods goods) {
        System.out.println(goods);
        try {//Bug 需要修改完善
            fileIutputStream = new FileInputStream(file);
            inputStream = new ObjectInputStream(fileIutputStream);
            list1 = (ArrayList<Goods>) inputStream.readObject();
            if (list1 == null) {
                list1 = new ArrayList<>();
            }
            list1.add(goods);//有2个对象了
            fileOutputStream = new FileOutputStream(file);
            outputStream = new ObjectOutputStream(fileOutputStream);
            outputStream.writeObject(list1);
            outputStream.flush();//输出流写完后刷新缓冲区
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("商品入库成功！");
    }

    public void updateGoods(Goods goods) {
        try {//异常监视
            fileIutputStream = new FileInputStream(file);
            inputStream = new ObjectInputStream(fileIutputStream);
            list1 = (ArrayList<Goods>) inputStream.readObject();
            int flag = 0;
            for (int i = 0; i < list1.size(); i++) {
                Goods goodsTemp = list1.get(i);
                //判断原来集合里面的商品和要修改的商品的ID是否相等
                if (goodsTemp.getGoodsId().equals(goods.getGoodsId())) {
                    list1.remove(i);//删除原来的
                    list1.add(goods);//添加新的
                    flag = 1;
                    break;
                }
            }
            if (flag == 1) {
                fileOutputStream = new FileOutputStream(file);//输出流，写文件
                outputStream = new ObjectOutputStream(fileOutputStream);
                outputStream.writeObject(list1);
                outputStream.flush();
                System.out.println("修改商品成功!");
            } else {
                System.out.println("查无此商品");
            }

        } catch (Exception e) {//异常捕捉
            System.out.println("修改商品出错，请检查！" + e.getMessage());
        }
    }

    public void deleteGoods(String goodsID) {
        try {//异常监视
            fileIutputStream = new FileInputStream(file);
            inputStream = new ObjectInputStream(fileIutputStream);
            list1 = (ArrayList<Goods>) inputStream.readObject();
            int flag = 0;
            for (int i = 0; i < list1.size(); i++) {
                Goods goods = list1.get(i);
                if (goods.getGoodsId().equals(goodsID)) {
                    list1.remove(i);
                    flag = 1;
                }
            }
            if (flag == 1) {
                fileOutputStream = new FileOutputStream(file);//输出流，写文件
                outputStream = new ObjectOutputStream(fileOutputStream);
                outputStream.writeObject(list1);
                outputStream.flush();
                System.out.println("删除商品成功!");
            } else {
                System.out.println("查无此商品");
            }

        } catch (Exception e) {//异常捕捉
            System.out.println("删除商品出错，请检查！" + e.getMessage());
        }

    }

    public void findGoodsByID(String goodsID) {
        try {
            fileIutputStream = new FileInputStream(file);
            inputStream = new ObjectInputStream(fileIutputStream);
            int flag = 0;
            ArrayList<Goods> list1 = (ArrayList<Goods>) inputStream.readObject();
            System.out.println(list1);
            for (Goods goods : list1) {//foreach 适合遍历数组和集合使用
                if (goods.getGoodsId().equals(goodsID)) {
                    System.out.println("已找到您要查询的商品");
                    System.out.println(goods);
                    flag = 1;
                    break;
                }
            }
            if (flag == 0) {
                System.out.println("查无此商品！");
            }
//            System.out.println("--------------");
//            for (int i = 0; i < list1.size(); i++) {// fori 循环，适合事先知道循环次数的方法
//                System.out.println(list1.get(i));
//            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void findAllGoods() {

    }
    public Goods findGoodsByIDForUpdate(String goodsID) {
        try {
            fileIutputStream = new FileInputStream(file);
            inputStream = new ObjectInputStream(fileIutputStream);
            int flag = 0;
            ArrayList<Goods> list1 = (ArrayList<Goods>) inputStream.readObject();
            for (Goods goods : list1) {//foreach 适合遍历数组和集合使用
                if (goods.getGoodsId().equals(goodsID)) {
                    flag = 1;
                    return goods;
                }
            }
            if (flag == 0) {
                return null;
            }
//
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return  null;
    }
}
